package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RequestApprovalBean;
import com.attendance.www.hr.beans.RequestApprovalPageBean;
import com.attendance.www.hr.beans.RequestApprovalParamBean;
import com.attendance.www.hr.beans.RequestApprovalSaveBean;
import com.attendance.www.hr.beans.RequestBean;
import com.attendance.www.hr.beans.RetroactiveApprovalBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 申请审批功能
 * @version 2017/11/04
 * @author 万波
 *
 */
@Controller
@RequestMapping("/requestApproval")
public class RequestApprovalController {

	@Resource
	private IApprovalService approvalService;

	/**
	 * 获取申请记录列表
	 * @param request
	 * @param paramBean
	 * @return
	 */
	@RequestMapping(value = "/getRequestApprovalList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestApprovalList(HttpServletRequest request,@RequestBody RequestApprovalParamBean paramBean) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}

		Integer pageNo = paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		
		if (pageNo ==null || pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		paramBean.setStartNum(startNum);
		
		//获取申请列表
		RequestApprovalPageBean requestApprovalPageBean = approvalService.getRequestList(paramBean,userEmployee.getEmployeeId());
		return ResultMap.success(requestApprovalPageBean);
	}
	
	/**
	 * 获取申请记录列表
	 * @param request
	 * @param paramBean
	 * @return
	 */
	@RequestMapping(value = "/getRequestApprovalListByRoldId.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestApprovalListByRoldId(HttpServletRequest request,@RequestBody RequestApprovalParamBean paramBean) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}

		Integer pageNo = paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		
		if (pageNo ==null || pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		paramBean.setStartNum(startNum);
		
		//获取申请列表
		RequestApprovalPageBean requestApprovalPageBean = approvalService.getRequestListByRoleId(paramBean,userEmployee.getEmployeeId());
		return ResultMap.success(requestApprovalPageBean);
	}
	
	/**
	 * 获取申请记录列表
	 * @param request
	 * @param requestId
	 * @return
	 */
	@RequestMapping(value = "/getRequestApprovalById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestApprovalById(HttpServletRequest request,@RequestBody Integer requestId) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//获取申请列表
		RequestApprovalBean requestApproval = approvalService.getRequestApprovalById(requestId);
	
		return ResultMap.success(requestApproval);
	}
	
	/**
	 * 申请批量审批
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/doApprovalRequestList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object doApprovalRequestList(HttpServletRequest request,@RequestBody RequestApprovalSaveBean requestApprovalSaveBean) {
		//取得登录用户信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//批量审批
		if (requestApprovalSaveBean.getAuth()==null) {
			requestApprovalSaveBean.setAuth(1);
		}
		if(userEmployee.getPrivilegeGroupId() !=null && 
				(userEmployee.getPrivilegeGroupId()==4 || ((userEmployee.getPrivilegeGroupId()==5) && requestApprovalSaveBean.getAuth()==5))){
			approvalService.saveApprovalRequestList(requestApprovalSaveBean,userEmployee.getEmployeeId(),true);
		}else{
			approvalService.saveApprovalRequestList(requestApprovalSaveBean,userEmployee.getEmployeeId(),false);
		}
	
		return ResultMap.success();
	}
	
	/**
	 * 补签审批操作
	 * @param request
	 * @param RetroactiveApprovalBean
	 * @return
	 */
	@RequestMapping(value = "/saveRetroactiveApproval.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRetroactiveApproval(HttpServletRequest request,@RequestBody RetroactiveApprovalBean retroactiveApprovalBean) {
		//取得登录用户自己岗位信息
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//验证必填项
		if (retroactiveApprovalBean.getApprovalState()==null 
				|| retroactiveApprovalBean.getRetroactiveIdList()==null 
				|| retroactiveApprovalBean.getRetroactiveIdList().size()<=0) {
			throw new ResultFaultException("0_00001");
		}
		
		if(approvalService.saveRetroactiveApproval(retroactiveApprovalBean,userEmployee.getEmployeeId())){
			return ResultMap.success();
		}else{
			throw new ResultFaultException("2_00002");
		}
	}
	/**
	 * 获取补签结果
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getApprovalRecordById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getApprovalRecordById(HttpServletRequest request) {
		String retroactiveIdToString = request.getParameter("retroactiveId");
		if (StringUtils.isBlank(retroactiveIdToString)){
			throw new ResultFaultException("0_00001");
		}
		Integer retroactiveId = Integer.valueOf(retroactiveIdToString);
		return ResultMap.success(approvalService.getApprovalRecordById(retroactiveId));
	}
	
	/**
	 * 获取多条补签结果--zx11-27
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getApprovalRecordByAttendanceId.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getApprovalRecordByAttendanceId(HttpServletRequest request) {
		String attendanceIdToString = request.getParameter("attendanceId");
		if (StringUtils.isBlank(attendanceIdToString)){
			throw new ResultFaultException("0_00001");
		}
		Integer attendanceId = Integer.valueOf(attendanceIdToString);
		return ResultMap.success(approvalService.getApprovalRecordByAttendanceId(attendanceId));
	}
	
	/**
	 * 查询其它申请的审批信息--张鑫
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getApprovalRecordInfoById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getApprovalRecordInfoById(HttpServletRequest request) {
		String requestId = request.getParameter("requestId");
		if (StringUtils.isBlank(requestId)){
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(approvalService.getApprovalRecordInfoById(requestId));
	}
	
	/**
	 * 计算请假时长，分钟--张鑫
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getRequestHours.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestHours(HttpServletRequest request, @RequestBody RequestBean requestBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		requestBean.setEmployeeId(userEmployee.getEmployeeId());
		if (requestBean.getStartDate()==null
			||requestBean.getEndDate()==null
			||requestBean.getStartTime()==null
			||requestBean.getEndTime()==null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(approvalService.getRequestMinutes(requestBean));
	}
	
	/**
	 * 计算早上加班时长（分钟）--张鑫
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getMorningMinutes.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getMorningMinutes(HttpServletRequest request, @RequestBody RequestBean requestBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		requestBean.setEmployeeId(userEmployee.getEmployeeId());
		if (requestBean.getStartDate()==null
			||requestBean.getStartTime()==null
			||requestBean.getEndTime()==null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(approvalService.getMorningMinutes(requestBean));
	}
	
}
